package com.itheima.service.empl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Employee;
import com.itheima.dao.EmployeeDao;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;



@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;

    /**
     * 查寻全部员工
     * @param employee
     * @return
     */
    @Override
    public Employee findAll(Employee employee) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        String code = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        lqw.eq(Employee::getPassword, code);
        return employeeDao.selectOne(lqw);
    }

    /**
     * 添加员工操作
     * @param employee
     * @return
     */
    @Override
    public int add(Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setStatus(1);
        return employeeDao.insert(employee);

    }

    /**
     * 分页操作员工表
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public IPage<Employee> findPage(int page, int pageSize, String name) {

        LambdaQueryWrapper<Employee> law = new LambdaQueryWrapper<>();
        IPage<Employee> pag = new Page<>(page, pageSize);
        law.like(name != null, Employee::getName, name);
        return employeeDao.selectPage(pag, law);
    }

    /**
     * 修改员工操作
     * @param employee
     * @return
     */
    @Override
    public int updateStatus(Employee employee) {

        return employeeDao.updateById(employee);
    }

    /**
     * 通过id查找员工
     * @param id
     * @return
     */
    @Override
    public Employee findById(long id) {
        return employeeDao.selectById(id);
    }
}
